package com.github.icloudpay.crm.core.biz;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.icloudpay.crm.core.entity.Consumer;
import com.github.icloudpay.crm.core.mapper.ConsumerMapper;
import com.github.wxiaoqi.security.common.msg.ResponseCode;
import com.github.wxiaoqi.security.common.util.MD5;
import com.github.wxiaoqi.security.common.util.SHA256Utils;

/**
 * 密码相关服务
 *
 * @author centerroot
 * @email lfw6699@163.com
 * @date 2018-05-17 15:32:14
 */
@Service
public class PasswordBiz {
	private static final Logger logger = LoggerFactory.getLogger(PasswordBiz.class);
	@Autowired
	private ConsumerMapper consumerMapper;
	@Autowired
	private SmsGateWayBiz smsGateWayBiz;
	/**
	 * 设置交易密码
	 * 2、判断手机号和验证码正确性
	 * 3、设置交易密码
	 * @author centerroot
	 * @time 创建时间:2018年5月22日下午2:55:13
	 * @param param
	 * @return
	 */
	public Map<String, Object> setPayPassword(Map<String, Object> param){
		logger.info("设置交易密码  请求参数：{}",param);
		Map<String, Object> respMap = new HashMap<String, Object>();
		String userNo = (String) param.get("userNo");
		String mobile = (String) param.get("mobile");
		String smsCode = (String) param.get("smsCode");
		String paymentPassword = (String) param.get("paymentPassword");
		
		Consumer consumer = consumerMapper.selectByPrimaryKey(userNo);
		if (null != consumer) {
			Map<String, Object> smsMap = new HashMap<String, Object>();
			smsMap.put("phone", mobile);
			smsMap.put("code", smsCode);
			Map<String, Object> smsMapResp = smsGateWayBiz.verificationCode(param);
			// 判断手机号和验证码正确性
			if (ResponseCode.OK.getCode().equals(smsMapResp.get("code")) && (Boolean)smsMapResp.get("verifyStatus")) {
				String payPWD = SHA256Utils.SHA256(paymentPassword+userNo);
				consumer.setPayPassword(payPWD);
				consumer.setPayPasswordStatus("00");
				consumer.setUpdatetime(new Timestamp(System.currentTimeMillis()));
				consumerMapper.updateByPrimaryKey(consumer);
				
				respMap.put("code", ResponseCode.OK.getCode());
				respMap.put("msg", ResponseCode.OK.getMessage());
			} else {
				respMap.put("code", ResponseCode.SMS_CODE_MATCH_FAIL.getCode());
				respMap.put("msg", ResponseCode.SMS_CODE_MATCH_FAIL.getMessage());
			}
		}else{
			respMap.put("code", ResponseCode.USER_NOTEXIST.getCode());
			respMap.put("msg", ResponseCode.USER_NOTEXIST.getMessage());
		}
			
		logger.info("设置交易密码   响应参数：{}",respMap);
		return respMap;
	}
	
	
	/**
	 * 修改支付密码
	 * 2、判断原交易密码正确
	 * 3、判断手机号和验证码正确
	 * 4、更改交易密码
	 * @author centerroot
	 * @time 创建时间:2018年5月22日下午3:27:49
	 * @param param
	 * @return
	 */
	public Map<String, Object> updatePayPassword(Map<String, Object> param){
		logger.info("修改支付密码  请求参数：{}",param);
		Map<String, Object> respMap = new HashMap<String, Object>();
		String userNo = (String) param.get("userNo");
		String mobile = (String) param.get("mobile");
		String smsCode = (String) param.get("smsCode");
		String oldPaymentPassword = (String) param.get("oldPaymentPassword");
		String paymentPassword = (String) param.get("paymentPassword");
		
		Consumer consumer = consumerMapper.selectByPrimaryKey(userNo);
		if (null != consumer) {
			String oldPayPWD = SHA256Utils.SHA256(oldPaymentPassword+userNo);
			if (oldPayPWD.equals(consumer.getPayPassword())) {
				Map<String, Object> smsMap = new HashMap<String, Object>();
				smsMap.put("phone", mobile);
				smsMap.put("code", smsCode);
				Map<String, Object> smsMapResp = smsGateWayBiz.verificationCode(param);
				// 判断手机号和验证码正确性
				if (ResponseCode.OK.getCode().equals(smsMapResp.get("code")) && (Boolean)smsMapResp.get("verifyStatus")) {
					String payPWD = SHA256Utils.SHA256(paymentPassword+userNo);
					consumer.setPayPassword(payPWD);
					consumer.setPayPasswordStatus("00");
					consumer.setUpdatetime(new Timestamp(System.currentTimeMillis()));
					consumerMapper.updateByPrimaryKey(consumer);
					
					respMap.put("code", ResponseCode.OK.getCode());
					respMap.put("msg", ResponseCode.OK.getMessage());
				} else {
					respMap.put("code", ResponseCode.SMS_CODE_MATCH_FAIL.getCode());
					respMap.put("msg", ResponseCode.SMS_CODE_MATCH_FAIL.getMessage());
				}
			} else {
				respMap.put("code", ResponseCode.PAY_PWD_ERROR.getCode());
				respMap.put("msg", ResponseCode.PAY_PWD_ERROR.getMessage());
			}
		}else{
			respMap.put("code", ResponseCode.USER_NOTEXIST.getCode());
			respMap.put("msg", ResponseCode.USER_NOTEXIST.getMessage());
		}
			
		logger.info("修改支付密码   响应参数：{}",respMap);
		return respMap;
	}
	
	/**
	 * 修改登录密码
	 * 2、判断用户密码锁定状态
	 * 3、判断原登录密码正确
	 * 4、更改登录密码
	 * @author centerroot
	 * @time 创建时间:2018年5月22日下午3:27:49
	 * @param param
	 * @return
	 */
	public Map<String, Object> updatePassword(Map<String, Object> param){
		logger.info("修改登录密码  请求参数：{}",param);
		Map<String, Object> respMap = new HashMap<String, Object>();
		String userNo = (String) param.get("userNo");
		String oldPassword = (String) param.get("oldPassword");
		String password = (String) param.get("password");
		
		Consumer consumer = consumerMapper.selectByPrimaryKey(userNo);
		if (null != consumer) {
			if ("00".equals(consumer.getPasswordStatus())) {
				String oldPWD = MD5.sign(oldPassword, consumer.getSaltvalue(), "utf-8");
				if (oldPWD.equals(consumer.getPassword())) {
					String newPassword = MD5.sign(password, consumer.getSaltvalue(), "utf-8");
					consumer.setPassword(newPassword);
					consumer.setUpdatetime(new Timestamp(System.currentTimeMillis()));
					consumerMapper.updateByPrimaryKey(consumer);
					
					respMap.put("code", ResponseCode.OK.getCode());
					respMap.put("msg", ResponseCode.OK.getMessage());
				} else {
					respMap.put("code", ResponseCode.PAY_PWD_ERROR.getCode());
					respMap.put("msg", ResponseCode.PAY_PWD_ERROR.getMessage());
				}
			} else {
				respMap.put("code", ResponseCode.LOCK_USER_PWD.getCode());
				respMap.put("msg", ResponseCode.LOCK_USER_PWD.getMessage());
			}
		}else{
			respMap.put("code", ResponseCode.USER_NOTEXIST.getCode());
			respMap.put("msg", ResponseCode.USER_NOTEXIST.getMessage());
		}
			
		logger.info("修改登录密码   响应参数：{}",respMap);
		return respMap;
	}
	
}